{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "input_step_size = 50\n",
    "output_size = 30\n",
    "sliding_window = False\n",
    "file_name= 'bitcoin2012_2017_50_30_prediction.h5' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume_(BTC)</th>\n",
       "      <th>Volume_(Currency)</th>\n",
       "      <th>Weighted_Price</th>\n",
       "      <th>Datetime</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1457422</th>\n",
       "      <td>1412763240</td>\n",
       "      <td>345.00</td>\n",
       "      <td>345.00</td>\n",
       "      <td>342.02</td>\n",
       "      <td>342.02</td>\n",
       "      <td>16.889610</td>\n",
       "      <td>5809.255122</td>\n",
       "      <td>343.954367</td>\n",
       "      <td>2014-10-08 10:14:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457423</th>\n",
       "      <td>1412763300</td>\n",
       "      <td>344.65</td>\n",
       "      <td>346.00</td>\n",
       "      <td>344.65</td>\n",
       "      <td>345.00</td>\n",
       "      <td>134.615730</td>\n",
       "      <td>46462.950245</td>\n",
       "      <td>345.152458</td>\n",
       "      <td>2014-10-08 10:15:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457424</th>\n",
       "      <td>1412763360</td>\n",
       "      <td>345.00</td>\n",
       "      <td>345.01</td>\n",
       "      <td>344.01</td>\n",
       "      <td>345.01</td>\n",
       "      <td>31.092182</td>\n",
       "      <td>10726.059939</td>\n",
       "      <td>344.976103</td>\n",
       "      <td>2014-10-08 10:16:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457425</th>\n",
       "      <td>1412763420</td>\n",
       "      <td>345.00</td>\n",
       "      <td>345.97</td>\n",
       "      <td>345.00</td>\n",
       "      <td>345.24</td>\n",
       "      <td>15.247212</td>\n",
       "      <td>5266.548725</td>\n",
       "      <td>345.410602</td>\n",
       "      <td>2014-10-08 10:17:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457426</th>\n",
       "      <td>1412763480</td>\n",
       "      <td>345.24</td>\n",
       "      <td>346.00</td>\n",
       "      <td>345.00</td>\n",
       "      <td>345.99</td>\n",
       "      <td>33.723594</td>\n",
       "      <td>11648.091077</td>\n",
       "      <td>345.398864</td>\n",
       "      <td>2014-10-08 10:18:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Timestamp    Open    High     Low   Close  Volume_(BTC)  \\\n",
       "1457422  1412763240  345.00  345.00  342.02  342.02     16.889610   \n",
       "1457423  1412763300  344.65  346.00  344.65  345.00    134.615730   \n",
       "1457424  1412763360  345.00  345.01  344.01  345.01     31.092182   \n",
       "1457425  1412763420  345.00  345.97  345.00  345.24     15.247212   \n",
       "1457426  1412763480  345.24  346.00  345.00  345.99     33.723594   \n",
       "\n",
       "         Volume_(Currency)  Weighted_Price            Datetime  \n",
       "1457422        5809.255122      343.954367 2014-10-08 10:14:00  \n",
       "1457423       46462.950245      345.152458 2014-10-08 10:15:00  \n",
       "1457424       10726.059939      344.976103 2014-10-08 10:16:00  \n",
       "1457425        5266.548725      345.410602 2014-10-08 10:17:00  \n",
       "1457426       11648.091077      345.398864 2014-10-08 10:18:00  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/bitstampUSD_1-min_data_2012-01-01_to_2017-05-31.csv').dropna().tail(1000000)\n",
    "df['Datetime'] = pd.to_datetime(df['Timestamp'],unit='s')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000000,)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices= df.loc[:,'Close'].values\n",
    "times = df.loc[:,'Datetime'].values\n",
    "prices.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "outputs = []\n",
    "inputs = []\n",
    "output_times = []\n",
    "input_times = []\n",
    "if sliding_window:\n",
    "    for i in range(len(prices)-input_step_size-output_size):\n",
    "        inputs.append(prices[i:i + input_step_size])\n",
    "        input_times.append(times[i:i + input_step_size])\n",
    "        outputs.append(prices[i + input_step_size: i + input_step_size+ output_size])\n",
    "        output_times.append(times[i + input_step_size: i + input_step_size+ output_size])\n",
    "else:\n",
    "    for i in range(0,len(prices)-input_step_size-output_size, input_step_size):\n",
    "        inputs.append(prices[i:i + input_step_size])\n",
    "        input_times.append(times[i:i + input_step_size])\n",
    "        outputs.append(prices[i + input_step_size: i + input_step_size+ output_size])\n",
    "        output_times.append(times[i + input_step_size: i + input_step_size+ output_size])\n",
    "inputs= np.array(inputs)\n",
    "outputs= np.array(outputs)\n",
    "output_times = np.array(output_times)\n",
    "input_times = np.array(input_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with h5py.File(file_name, 'w') as f:\n",
    "    f.create_dataset(\"inputs\", data = inputs)\n",
    "    f.create_dataset('outputs', data = outputs)\n",
    "#     f.create_dataset(\"input_times\", data = input_times)\n",
    "#     f.create_dataset('output_times', data = output_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.save(file_name[:-3] + 'input_times',input_times)\n",
    "np.save(file_name[:-3] + 'output_times',output_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2014-10-08T10:14:00.000000000', '2014-10-08T10:15:00.000000000',\n",
       "       '2014-10-08T10:16:00.000000000', ...,\n",
       "       '2017-05-30T23:58:00.000000000', '2017-05-30T23:59:00.000000000',\n",
       "       '2017-05-31T00:00:00.000000000'], dtype='datetime64[ns]')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
